package theMarshall;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import data.Cal;
import data.Task;
import data.User;

public class TaskMarshallUtil {
	
	static JAXBContext jaxbContext;
	static Cal cal;
	static String path = "/home/hectorbernal/Dropbox/MDS2/taskMarshall/webData/task-manager-xml.xml";
	
	public TaskMarshallUtil() throws IOException{
	try{// assign path to the task-manager Xml, 
        

        // create an instance context class, to serialize/deserialize.
        jaxbContext = JAXBContext.newInstance(Cal.class);

        // Create a file input stream for the task Xml.
        FileInputStream stream = new FileInputStream(path);

        // deserialize task-manager xml into java objects.
        cal = (Cal) jaxbContext.createUnmarshaller().unmarshal(stream);

   
	}
	catch (JAXBException ex) {
	    Logger.getLogger(TaskMarshall.class.getName()).log(Level.SEVERE, null, ex);
	}
} 

	

	public static void PrintTaskObject() {
		for (Task task : cal.tasks) {
			try {

				StringWriter writer = new StringWriter();

				// create a context object for Student Class
				JAXBContext jaxbContext = JAXBContext.newInstance(Task.class);

				// Call marshal method to serialize student object into Xml
				jaxbContext.createMarshaller().marshal(task, writer);

				System.out.println(writer.toString());

			} catch (JAXBException ex) {
				Logger.getLogger(TaskMarshall.class.getName()).log(
						Level.SEVERE, null, ex);
			}
		}

	}

    public static void PrintUserObject(User user) {

        try {


            StringWriter writer = new StringWriter();

            // create a context object for Student Class
            JAXBContext jaxbContext = JAXBContext.newInstance(User.class);

            // Call marshal method to serialize student object into Xml
            jaxbContext.createMarshaller().marshal(user, writer);

            System.out.println(writer.toString());

        } catch (JAXBException ex) {
            Logger.getLogger(TaskMarshall.class.getName()).log(Level.SEVERE, null, ex);
        }




    }
    
    public static void close() throws IOException, JAXBException {
    	// Serialize university object into xml.
        
        StringWriter writer = new StringWriter();

        // We can use the same context object, as it knows how to 
        //serialize or deserialize University class.
        jaxbContext.createMarshaller().marshal(cal, writer);

        
        System.out.println("Printing serialized cal Xml before saving into file!");
        
        // Print the serialized Xml to Console.
        System.out.println(writer.toString());
        
        
        // Finally save the Xml back to the file.
        SaveFile(writer.toString(), path);

    }

    private static void SaveFile(String xml, String path) throws IOException {

        File file = new File(path);

        // create a bufferedwriter to write Xml
        BufferedWriter output = new BufferedWriter(new FileWriter(file));

        output.write(xml);

        output.close();
    }

	
}
